package com.xyjf.code.util;


/**
 * 生成的校验码
 */
public class CheckCodeUtil {

    public static char checkSum(byte[] buf, int size) {
        long ckSum = 0;
        for (int i = 0; i < size - 1; i += 2) {
            ckSum += (((buf[i + 1] << 8) & 0xff00) ^ (buf[i] & 0xff));
        }
        if (size % 2 == 1) {
            ckSum += buf[size - 1] & 0xff;
        }
        ckSum = (ckSum >> 16) + (ckSum & 0xffff);
        ckSum += (ckSum >> 16);
        return (char) (~ckSum);
    }


    public static int getEndCode(Long id){
        char code=checkSum(id.toString().getBytes(),1);
        int num=(int)code;
        int sumNum=getSumCode(num);
        return sumNum%10;
    }


    public static int getSumCode(int num){
        int sum=0;
        while(num!=0){
            sum = sum + num%10;
            num = num/10;
        }
        return  sum;
    }

    public  static Long getId(Long id,String apiId){
        Long baseId=Long.parseLong(id+""+apiId);
        int endId=getEndCode(baseId);
        return  Long.parseLong(baseId+""+endId);
    }


    public  static  void main(String[] args){
        Long id=3055856354631681l;
        int num=getEndCode(id);
        System.out.print(num);
    }
}
